package com.zhuiyun.project.api.system.role.controller;

import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.system.role.entity.SysRole;
import com.zhuiyun.project.api.system.role.service.SysRoleService;
import com.zhuiyun.project.api.system.user.entity.SysUser;
import com.zhuiyun.project.api.system.user.entity.SysUserRole;
import com.zhuiyun.project.api.system.user.service.SysUserService;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.constants.CommonConstants;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName SysRoleController
 * @Description TODO
 * @Author Acer
 * @Date 2023/10/25 13:36
 **/
@RequestMapping("/system/role")
@RestController
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysUserService userService;

    /**
      * @Author hyp
      * @Description 角色详情分页查询
      * @Date 2023/10/26 14:38
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param pageRequest
      * @param sysRole
    */
    @ApiOperation(value = "角色详情分页查询")
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @OptLog(modular = "角色管理",type = CommonConstants.SELECT,describe = "角色详情分页查询")
    @GetMapping("/list")
    public CommonResult listRoles(PageRequest pageRequest, SysRole sysRole){
        return CommonResult.ok(roleService.listRoles(pageRequest,sysRole));
    }


    /**
      * @Author hyp
      * @Description 根据角色编号删除角色信息
      * @Date 2023/10/26 14:36
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param roleIds
    */
    @ApiOperation(value = "根据角色编号删除角色信息")
    @OptLog(modular = "角色管理", type = CommonConstants.DELETE ,describe = "根据角色编号删除角色信息")
    @PreAuthorize("ss.hasPermi('system:role:remove')")
    @DeleteMapping("/{roleIds}")
    public CommonResult removeRoleByIds(@PathVariable("roleIds") Long[] roleIds){
        return roleService.removeRoleByIds(roleIds);
    }

    /**
      * @Author hyp
      * @Description 新增角色信息
      * @Date 2023/10/26 14:36
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param role
    */
    @ApiOperation(value = "新增角色信息")
    @OptLog(modular = "角色管理",type = CommonConstants.ADD,describe = "新增角色信息")
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @PostMapping
    public CommonResult addRole(@Validated @RequestBody SysRole role){

        if (!roleService.checkRoleNameUnique(role)){
            return CommonResult.error("新增角色" + role.getRoleName() + "失败，角色名已存在");
        }else if (!roleService.checkRoleKeyUnique(role)){
            return CommonResult.error("新增角色" + role.getRoleKey() + "失败，角色权限已存在");
        }
        return CommonResult.ok(roleService.addRole(role));
    }

    /**
      * @Author hyp
      * @Description 根据角色编号查询角色信息
      * @Date 2023/10/26 14:35
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param roleId
    */
    @ApiOperation(value = "根据角色编号查询角色信息")
    @OptLog(modular = "角色管理",type = CommonConstants.SELECT,describe = "根据角色编号获取信息")
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping("/{roleId}")
    public CommonResult getInfoRoleById(@PathVariable("roleId") Long roleId){
        return CommonResult.ok(roleService.getInfoRoleById(roleId));
    }

    /**
      * @Author hyp
      * @Description 修改角色信息
      * @Date 2023/10/26 14:34
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param role
    */
    @ApiOperation(value = "修改角色信息")
    @OptLog(modular = "角色管理",type = CommonConstants.UPDATE,describe = "修改角色信息")
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @PutMapping
    public CommonResult editRole(@Validated @RequestBody SysRole role){

        roleService.checkRoleAllowed(role);
        if (!roleService.checkRoleNameUnique(role)){
            return CommonResult.error("新增角色" + role.getRoleName() + "失败，角色名已存在");
        }else if (!roleService.checkRoleKeyUnique(role)){
            return CommonResult.error("新增角色" + role.getRoleKey() + "失败，角色权限已存在");
        }
        if ( roleService.editRole(role) > 0){
            return CommonResult.ok(EmErrorCode.UPDATE_OK);
        }
        return CommonResult.error("修改角色" + role.getRoleName() + "失败");
    }


    /**
      * @Author hyp
      * @Description 角色状态修改
      * @Date 2023/10/26 14:34
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param role
    */
    @ApiOperation(value = "角色状态修改")
    @OptLog(modular = "角色管理",type = CommonConstants.UPDATE,describe = "角色状态修改")
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @PutMapping("/changeStatus")
    public CommonResult changeStatusRole(@RequestBody SysRole role){
        roleService.checkRoleAllowed(role);
        return CommonResult.ok(roleService.changeStatusRole(role));
    }



    /**
      * @Author hyp
      * @Description 查询已分配用户角色列表
      * @Date 2023/10/26 14:33
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param pageRequest
      * @param user
    */
    @ApiOperation(value = "查询已分配用户角色列表")
    @OptLog(modular = "角色管理",type = CommonConstants.SELECT,describe = "查询已分配用户角色列表")
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/authUser/allocatedList")
    public CommonResult allocatedUserList(PageRequest pageRequest, SysUser user){
        return CommonResult.ok(userService.selectAllocatedList(pageRequest,user));
    }

    /**
      * @Author hyp
      * @Description 查询未分配用户角色列表
      * @Date 2023/10/26 14:33
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param pageRequest
      * @param user
    */
    @ApiOperation(value = "查询未分配用户角色列表")
    @OptLog(modular = "角色管理",type = CommonConstants.SELECT,describe = "查询未分配用户角色列表")
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/authUser/unallocatedList")
    public CommonResult unallocatedUserList(PageRequest pageRequest,SysUser user){
        return CommonResult.ok(userService.selectUnallocatedList(pageRequest,user));
    }

    /**
      * @Author hyp
      * @Description 批量选择用户授权
      * @Date 2023/10/26 14:32
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param roleId
      * @param userIds
    */
    @ApiOperation(value = "批量选择用户授权")
    @OptLog(modular = "角色管理",type = CommonConstants.ADD,describe = "批量选择用户授权")
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @PutMapping("/authUser/selectAll")
    public CommonResult selectAuthUserAll(Long roleId,Long[] userIds){
        return CommonResult.ok(roleService.selectAuthUserAll(roleId,userIds));
    }

    /**
      * @Author hyp
      * @Description 取消授权用户
      * @Date 2023/10/26 14:31
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param userRole
    */
    @ApiOperation(value = "取消授权用户")
    @OptLog(modular = "角色管理",type = CommonConstants.UPDATE,describe = "取消授权用户")
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @PutMapping("/authUser/cancel")
    public CommonResult cancelAuthUser(@RequestBody SysUserRole userRole){
        return CommonResult.ok(roleService.cancelAuthUser(userRole));
    }
    /**
      * @Author hyp
      * @Description 批量取消授权用户
      * @Date 2023/10/26 14:31
      * @Return com.zhuiyun.project.common.response.CommonResult
      * @param roleId
      * @param userIds
    */
    @ApiOperation(value = "批量取消授权用户")
    @OptLog(modular = "角色管理",type = CommonConstants.UPDATE,describe = "批量取消用户授权")
    @PreAuthorize("@ss.haspermi('system:role:edit')")
    @PutMapping("/authUser/cancelAll")
    public CommonResult cancelAuthUserAll(Long roleId,Long[] userIds){
        return CommonResult.ok(roleService.cancelAuthUserAll(roleId,userIds));
    }
}
